<?php
session_start();
include_once "../apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();

$speciesName = $_POST["species_name"];
$speciesDetail = $_POST["species_detail"];

$speciesId = $_GET["species_id"];
if($speciesId == "" || $speciesId == null){
	$speciesId = $_POST["species_id"];
}

$mode = $_GET["mode"];
if($mode == "" || $mode == null){
	$mode = $_POST["mode"];
}

$page = $_GET['page'];
if (!isset($page) && $page == "") {
	$page = 1;
}

if($mode == "insert"){
	$arrData["species_name"] = "".$speciesName."";
	$arrData["species_detail"] ="".$speciesDetail."";
	$arrData["create_date"] = "".date("Y-m-d")."";
	$arrData["create_by"] = "".$_SESSION["user_name"]."";
	$managerIUDS->inserData($arrData, "species");
	header("Location:../contents/species_main.php");
}else if($mode == "delete"){
	$arrPrimaryKey["species_id"] = "".$speciesId."";
	$managerIUDS->deleteData("species", $arrPrimaryKey, "");
	$managerIUDS->deleteData("breed", $arrPrimaryKey, "");
	header("Location:../contents/species_main.php");
}else if($mode == "update"){
	$arrData["species_name"] = "".$speciesName."";
	$arrData["species_detail"] ="".$speciesDetail."";
	$arrData["update_date"] = "".date("Y-m-d")."";
	$arrData["update_by"] = "".$_SESSION["user_name"]."";
	$arrPrimaryKey["species_id"] = "".$speciesId."";
	//print_r($arrData);
	$managerIUDS->updateData($arrData, "species", $arrPrimaryKey, "");
	header("Location:../contents/species_main.php");
}else if($mode == "search"){
	$speciesName = $_GET["species_name"];
	header("Location:../contents/species_main.php?mode=search&page=".$page."&species_name=".urlencode($speciesName));
}else if($mode == "listSpeciesData"){
	$speciesData = array();
	$speciesName = $_GET["species_name"];
	if($speciesName == "" || $mode == null){
		$speciesName = $_POST["species_name"];
	}
	$speciesData = $managerIUDS->queryData("species_name", "species", " WHERE species_name LIKE '%".$speciesName."%'", "2", "");
	echo json_encode($speciesData);
}else if($mode == "getSpeciesById"){
	$petId = $_GET["pet_id"];
	if($petId == "" || $petId == null){
		$petId = $_POST["pet_id"];
	}
	$speciesId = $managerIUDS->queryData("species_id", "pet", " WHERE pet_id = '".$petId."'", "1", "");
	$speciesName = $managerIUDS->queryData("species_name", "species", " WHERE species_id = '".$speciesId["species_id"]."'", "1", "");
	echo json_encode($speciesName);
}
?>